//
//  UIView+Utils.m
//  刷刷
//
//  Created by 谭真 on 16/4/16.
//  Copyright © 2016年 圣巴(上海)文化传播有限公司. All rights reserved.
//

#import "UIView+Utils.h"

@implementation UIView (Utils)

#pragma mark - 纯文字的HUD

/// 显示一个纯文字的tipLable
- (void)showInfo:(NSString *)message {
    [SGInfoAlert showInfo:message bgColor:[[UIColor darkGrayColor] CGColor] fgColor:[[UIColor whiteColor] CGColor] inView:[UIApplication sharedApplication].keyWindow vertical:0.8];
}

/// 连续三次的动画效果 类似微博的点赞
+ (void)showOscillatoryAnimationWithLayer:(CALayer *)layer scale1:(CGFloat)scale1 scale2:(CGFloat)scale2 {
    [self showOscillatoryAnimationWithLayer:layer scale1:scale1 duration1:0.15 scale2:scale2 duration2:0.15];
}

+ (void)showOscillatoryAnimationWithLayer:(CALayer *)layer scale1:(CGFloat)scale1 duration1:(CGFloat)duration1 scale2:(CGFloat)scale2 duration2:(CGFloat)duration2 {
    [UIView animateWithDuration:duration1 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
        [layer setValue:@(scale1) forKeyPath:@"transform.scale"];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:duration2 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
            [layer setValue:@(scale2) forKeyPath:@"transform.scale"];
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.1 delay:0 options:UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionCurveEaseInOut animations:^{
                [layer setValue:@(1.0) forKeyPath:@"transform.scale"];
            } completion:nil];
        }];
    }];
}

/// 购物车动画飞入效果
+ (void)flyAnimationWithContentView:(UIView *)contentView fromX:(CGFloat)x y:(CGFloat)y {
    [self flyAnimationWithContentView:contentView fromX:x y:y toX:36 toY:mScreenHeight - 64 - 40];
}

+ (void)flyAnimationWithContentView:(UIView *)contentView fromX:(CGFloat)x y:(CGFloat)y toX:(CGFloat)toX toY:(CGFloat)toY{
    CAKeyframeAnimation *pathAnimation = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    pathAnimation.calculationMode = kCAAnimationCubic;
    pathAnimation.fillMode = kCAFillModeForwards;
    pathAnimation.removedOnCompletion = NO;
    pathAnimation.duration = 0.5;
    pathAnimation.repeatCount = 1;
    
    CGMutablePathRef curvedPath = CGPathCreateMutable();
    CGPathMoveToPoint(curvedPath, NULL, x, y);
    CGPathAddQuadCurveToPoint(curvedPath, NULL, x - 160, y - 120, toX, toY);
    
    pathAnimation.path = curvedPath;
    CGPathRelease(curvedPath);
    
    UIImageView *circleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"zhuangtai_sel"]];
    circleView.frame = CGRectMake(1, 1, 20, 20);
    [contentView addSubview:circleView];
    
    [circleView.layer addAnimation:pathAnimation forKey:@"moveTheSquare"];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [circleView removeFromSuperview];
    });
}

@end
